package com.zf.bm.common.core.sys.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zf.bm.common.constant.Constant;
import com.zf.bm.common.core.base.service.BaseService;
import com.zf.bm.common.core.config.ConfigUtils;
import com.zf.bm.common.core.sys.dao.UserDao;
import com.zf.bm.common.core.sys.entity.SysUserCert;
import com.zf.bm.common.core.sys.entity.User;
import com.zf.bm.common.utils.ValidatorUtil;
import com.zf.bm.modules.system.utils.SysInfoUtils;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@DS("master")
@Service
@CacheConfig(cacheNames = "userCache")
@Transactional(readOnly = true)
public class UserService extends BaseService<UserDao, User> {
    @Resource
    private SysUserCertService sysUserCertService;

    private static final String[] USERNAME_ARRAY = {"root", "yewu", "admin"};

    /**
     * 根据登录名称查询用户
     *
     * @param user
     * @return
     */
    public User getByLoginName(User user) {
        QueryWrapper<User> lqw = new QueryWrapper<User>().eq("login_name", user.getLoginName()).eq("del_flag",user.getDelFlag());
        return this.getOne(lqw);

    }


    /**
     * 删除用户角色关联数据
     *
     * @param user
     * @return
     */
    @CacheEvict(key = "#p0.loginName")
    public int deleteUserRole(User user) {
        return baseMapper.deleteUserRole(user.getId());
    }

    /**
     * 插入用户角色关联数据
     *
     * @param user
     * @return
     */
    @CachePut(key = "#p0.loginName")
    public int insertUserRole(User user) {
        return baseMapper.insertUserRole(user);
    }

    /**
     * 查询用户角色关联数据
     *
     * @return
     */
    public Long countUserRole(Long roleId) {
        return baseMapper.countUserRole(roleId);
    }

    @Transactional(readOnly = false)
    public void updateLoginFlag(String account, Date frozenDate) {
        LambdaUpdateWrapper updateWrapper = Wrappers.lambdaUpdate(new User())
                .set(User::getLoginFlag, ConfigUtils.NO)
                .set(User::getFrozenDate, frozenDate)
                .eq(User::getLoginName, account);
        update(updateWrapper);
    }

    @Transactional(readOnly = false)
    public void updateLoginFlag(String account, String loginFlag, Date frozenDate) {
        User user = new User();
        user.setLoginFlag(null);
        update(Wrappers.lambdaUpdate(user)
                .set(User::getLoginFlag, loginFlag)
                .set(User::getFrozenDate, frozenDate)
                .eq(User::getLoginName, account));
    }

    /**
     * 根据登录名查询该用户的角色
     *
     * @param loginName
     * @return
     */
    public String getRoleByLoginName(String loginName) {
        return baseMapper.getRoleByLoginName(loginName);

    }

    /**
     * 根据key查询用户
     *
     * @param usbKey
     * @return
     */
    public User getByLoginKey(String usbKey) {
        SysUserCert userCert = sysUserCertService.getOne(new LambdaQueryWrapper<SysUserCert>()
                .select(SysUserCert::getUserId).eq(SysUserCert::getCert, usbKey));

        return this.getOne(new LambdaQueryWrapper<User>().eq(User::getId,userCert.getUserId()));
    }

    /**
     * 是否需要跳转配置引导
     * @param name
     * @return
     */
    public boolean isGuide(String name) {
        String mcEditState = SysInfoUtils.getSysInfo(Constant.MC_EDIT_STATE);
        if (ValidatorUtil.isEmptyObj(mcEditState) || !"0".equals(mcEditState)) {
            return false;
        }

        if (ValidatorUtil.isEmptyObj(name)) {
            return false;
        }

        // root、yewu、admin用户首次登录需要跳转配置引导页面
        for (String username : USERNAME_ARRAY) {
            if (username.equals(name)) {
                return true;
            }
        }
        return false;
    }
}
